#include "columnnames.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>

ColumnNames::ColumnNames()
{
}

bool ColumnNames::loadColumnNames(const char *datName)
{
    colIndexMap.clear();

    std::string colFilePath = "./ColumnNames/";
    colFilePath += datName;

    FILE* file = fopen(colFilePath.c_str(), "r");
    if( !file )
    {
        return false;
    }

    while( !feof(file) )
    {
        int index = 0;
        char cData[100];
        if( fscanf( file, "%d %s",&index, cData ) == 2 )
        {
            index -= 1;
            std::string colName = cData;
            colIndexMap.insert( std::map< std::string, int >::value_type(colName, index) );
        }
    }
    fclose(file);
    return true;
}

unsigned int ColumnNames::getIndex(const char *colName)
{
    std::string colStr = colName;

    std::map< std::string, int >::iterator i = colIndexMap.find(colStr);

    if( i != colIndexMap.end() )
    {
        return i->second;
    }

    std::cerr<<"Failed to get index of column "<<colName<<std::endl;
    exit(1);

    return 999999;
}
